package org.aldar.bquest.sound;

import android.content.Context;
import org.aldar.bquest.profile.GameProfile;
import org.aldar.bquest.screens.MainBoardScreen;
import org.aldar.bquest.util.SQLiteHelper;
import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.music.MusicManager;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.audio.sound.SoundManager;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.util.debug.Debug;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Daria
 * Date: 4/14/13
 * Time: 10:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameSound {

    Context context;
    SoundManager soundManager;
    MusicManager musicManager;
    private Music mainLoop;
    private Sound skillStartSound;
    private Sound pageTurnSound;
    private Sound tileRefillSound;
    private Music[] ticTacSound = new Music[5];
    private float mainLoopVolume = 0.2f;
    private float mainLoopVolumeLow = 0.1f;
    private Sound crystalMatchSound;
    private Sound simpleMatchSound;
    private Sound abilityAppearSound;
    private Sound buttonClickSound;
    private Sound checkboxClickSound;
    private Sound popupSound;
    private static GameSound currentInstance = null;
    private Sound coinDropSound;
    private Sound earthquakeSound;


    public GameSound(Context context, SoundManager soundManager, MusicManager musicManager) {
        this.soundManager = soundManager;
        this.musicManager = musicManager;
        this.context = context;
    }

    public static void setCurrentInstance(GameSound instance) {
        currentInstance = instance;
    }

    public static GameSound getCurrentInstance() {
        return currentInstance;
    }


    /**
     * 游戏音效*
     */
    private Sound[] matchSound = new Sound[10];
    private Sound[] match2Sound = new Sound[10];
    private Sound bubbleMatchSound;
    private Sound magnetSkillSound;
    private Sound hasteSkillSound;
    private Sound explodeSkillSound;
    private Sound missileSkillSound;
    private Sound concertSkillSound;
    private Sound concertClapSound;
    private Sound popupWinSound;
    private Sound popupFailSound;
    private Sound noMovesLeftSound;


    /**
     * 初始化音效
     */
    public void initSound() {
        try {
            this.mainLoop = MusicFactory.createMusicFromAsset(musicManager, context, "raw/loop_2.ogg");
            matchSound[0] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_simple.ogg");
            matchSound[1] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match2.ogg");
            matchSound[2] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match3.ogg");
            matchSound[3] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match4.ogg");
            matchSound[4] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");
            matchSound[5] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");
            matchSound[6] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");
            matchSound[7] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");
            matchSound[8] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");
            matchSound[9] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match5.ogg");

            match2Sound[0] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_simple.ogg");
            match2Sound[1] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m2.ogg");
            match2Sound[2] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m3.ogg");
            match2Sound[3] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m4.ogg");
            match2Sound[4] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");
            match2Sound[5] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");
            match2Sound[6] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");
            match2Sound[7] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");
            match2Sound[8] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");
            match2Sound[9] = SoundFactory.createSoundFromAsset(soundManager, context, "raw/m5.ogg");

            bubbleMatchSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_bubble.ogg");
            crystalMatchSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_crystal_smooth.ogg");
            simpleMatchSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_simple.ogg");
            abilityAppearSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/ability_appear.ogg");
            tileRefillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_tiles_fall.ogg");

            magnetSkillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/skill_magnet.ogg");
            explodeSkillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/skill_explosion.ogg");
            hasteSkillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/skill_haste-1.ogg");
            hasteSkillSound.setVolume(100);
            skillStartSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/skill_mp3cut.ogg");
            pageTurnSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/page_turn.ogg");
            missileSkillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/skill_arrow_.ogg");
            missileSkillSound.setVolume(50);
            concertSkillSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/concert.ogg");
            concertClapSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/clapping.ogg");
            concertClapSound.setVolume(0.5f);
            popupFailSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/popup_fail.ogg");
            popupWinSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/popup_win.ogg");
            noMovesLeftSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/no_moves_left-1.ogg");
            buttonClickSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/btn_2.ogg");
            checkboxClickSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/checkbox.ogg");
            popupSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/match_simple.ogg");
            coinDropSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/coin-drop-4.ogg");
            earthquakeSound = SoundFactory.createSoundFromAsset(soundManager, context, "raw/earthquake.ogg");

            ticTacSound[0] = MusicFactory.createMusicFromAsset(musicManager, context, "raw/tictac1_.ogg");
            ticTacSound[1] = MusicFactory.createMusicFromAsset(musicManager, context, "raw/tictac2_.ogg");
            ticTacSound[2] = MusicFactory.createMusicFromAsset(musicManager, context, "raw/tictac3_.ogg");
            ticTacSound[3] = MusicFactory.createMusicFromAsset(musicManager, context, "raw/tictac4_.ogg");
            ticTacSound[4] = MusicFactory.createMusicFromAsset(musicManager, context, "raw/tictac5_.ogg");

            this.mainLoop.setVolume(mainLoopVolume);
            this.mainLoop.setLooping(true);


        } catch (final IOException e) {
            Debug.e("mGoodMusic Error", e);
        }
    }

    public void playSimpleMatchSound(int index) {
        //Log.i("sound", "startsound!!!");
        if (GameProfile.getSettingsPlaySounds(MainBoardScreen.getmSQLiteHelper())) {
            simpleMatchSound.play();
        }
    }

    public void playBubbleMatchSound(int index) {
        //Log.i("sound", "startsound!!!");
        if (GameProfile.getSettingsPlaySounds(MainBoardScreen.getmSQLiteHelper())) {
            bubbleMatchSound.play();
        }
    }

    public void playCrystalMatchSound(int index) {
        //Log.i("sound", "startsound!!!");
        if (GameProfile.getSettingsPlaySounds(MainBoardScreen.getmSQLiteHelper())) {
            crystalMatchSound.play();
        }
    }

    public void playMatch2Sound(int index) {
        //Log.i("sound", "startsound!!!");
        if (GameProfile.getSettingsPlaySounds(MainBoardScreen.getmSQLiteHelper())) {
            match2Sound[index].setVolume(1f);
            match2Sound[0].play();
        }
    }

    public void playMainLoopSound() {
        //Log.i("sound", "startsound!!!");
        if (GameProfile.getSettingsPlayMusic(MainBoardScreen.getmSQLiteHelper())) {
            if (mainLoop.isPlaying()) {
                mainLoop.resume();
            } else {
                mainLoop.play();
            }
            fadeMainLoop(true);
        } else {
            fadeMainLoop(false);
            //if (mainLoop.isPlaying())
            //    mainLoop.pause();
        }
    }

    public void stopMainLoopSound() {
        //Log.i("sound", "startsound!!!");
        if (mainLoop.isPlaying())
            mainLoop.pause();
    }

    public void playMagnetSkillSound() {
        stopAndPlaySound(magnetSkillSound);
    }

    private void stopAndPlaySound(Sound soundToPlay) {
        if (GameProfile.getSettingsPlaySounds(MainBoardScreen.getmSQLiteHelper())) {
            soundToPlay.stop();
            soundToPlay.play();
        }
    }

    public void playAbilityAppearSound() {
        stopAndPlaySound(abilityAppearSound);
    }

    public void playExplodeSkillSound() {
        stopAndPlaySound(explodeSkillSound);
    }

    public void playHasteSkillSound() {
        stopAndPlaySound(hasteSkillSound);
    }

    public void playStartSkillSound() {
        stopAndPlaySound(skillStartSound);
    }

    public void playTileRefillSound() {
        stopAndPlaySound(tileRefillSound);
    }

    public void playButtonClickSound() {
        stopAndPlaySound(buttonClickSound);
    }

    public void playCheckboxClickSound() {
        stopAndPlaySound(checkboxClickSound);
    }

    public void playPopupSound() {
        stopAndPlaySound(popupSound);
    }

    public void playConcertSkillSound() {
        stopAndPlaySound(concertSkillSound);
    }

    public void playConcertClapSound() {
        stopAndPlaySound(concertClapSound);
    }

    public void playMissileSkillSound() {
        stopAndPlaySound(missileSkillSound);
    }

    public void playNoMovesLeftSound() {
        stopAndPlaySound(noMovesLeftSound);
    }

    public void playLevelFailedSound() {
        stopAndPlaySound(popupFailSound);
    }

    public void playLevelCompletedSound() {
        stopAndPlaySound(popupWinSound);
    }

    public void playTurnPageSound() {
        stopAndPlaySound(pageTurnSound);
    }

    public void playCoinDropSound() {
        stopAndPlaySound(coinDropSound);
    }

    public void playEarthquakeSound() {
        stopAndPlaySound(earthquakeSound);
    }

    public void playTicTacSound(int movesLeft) {
        for (int i = 0; i < 5; i++) {
            if (ticTacSound[i].isPlaying())
                ticTacSound[i].pause();
        }
        if (GameProfile.getSettingsPlaySounds(SQLiteHelper.getCurrentInstance())) {
            ticTacSound[movesLeft - 1].setLooping(false);
            if (ticTacSound[movesLeft - 1].isPlaying()) {
                ticTacSound[movesLeft - 1].seekTo(0);
                ticTacSound[movesLeft - 1].resume();
            } else {
                ticTacSound[movesLeft - 1].seekTo(0);
                ticTacSound[movesLeft - 1].play();
            }
        }
    }

    public void stopTicTacSound() {
        for (int i = 0; i < 5; i++) {
            if (ticTacSound[i] != null && ticTacSound[i].isPlaying())
                ticTacSound[i].pause();
        }
    }

    public void setDefaultMainLoopVolume() {
        mainLoop.setVolume(mainLoopVolume);
    }

    public void setLowMainLoopVolume() {
        mainLoop.setVolume(mainLoopVolumeLow);
    }

    private void fadeMainLoop(final boolean musicOn) {
        for (int i = 0; i < 5; i++) {
            final int finalI = i;
            MainBoardScreen.getInstanceEngine().registerUpdateHandler(new TimerHandler(0.1f + 0.2f * i, new ITimerCallback() {
                @Override
                public void onTimePassed(TimerHandler pTimerHandler) {
                    if (musicOn) {
                        mainLoop.setVolume(mainLoopVolume * 0.2f * (finalI + 1));
                    } else {
                        mainLoop.setVolume(mainLoopVolume * 0.2f * (4 - finalI));
                    }
                }
            }));
        }
    }
}
